### Project 8 A Dimmable Table Lamp
**1.Description**
In this project, we will use an adjustable potentiometer to read the analog values so as to control the brightness of the LED. When setting the code, the analog values will range from 0 to 1023. The brightness of the LED is controlled by PWM value, which ranges from 0 to 255.
We need to use the mapping function to map the values 0-1023 to 0-255. For example, if we read an analog value of 512, then the output to the PWM value after mapping is 128. After successful setting, we can control the brightness of the LED on the module by rotating the potentiometer.
Task analysis
| Sensor | A Rotary Potentiometer and a LED Module |
| ------ | ------------------------------------------------------------ |
| Role | Cat |
| Place | Bedroom |
| Result | The brightness of the LED and the bedroom will change by adjusting the potentiometer |
**2.Rotary Potentiometer Introduction**

There are 6 ports on the Keyestudio LED module and those marked in the same name are connected. G is GND, V is VCC and S is a signal port. There is a red LED on the module, which can be used to achieve the effect of breathing by controlling the PWM output of the S port.
The rotary potentiometer module uses a 10K adjustable resistor. By rotating the potentiometer, we can change the value of the resistor and then build a circuit to convert the resistance change into the voltage change.

There are 6 ports on the module and those marked in the same name are connected. To link it with the control board, we only need to connect 3 ports, that’s G to GND, V to 5V and S to a signal port(A0~A5).
**Specifications**
| LED Module | Rotary Potentiometer |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| Operating voltage: DC 3.3-5V
Operating Current:60mA
Maximum power:300mW
Operating temperature:-25℃--65℃
Dimension: 30mm * 27mm * 5mm
Weight:2.0g
LED color: Red | Operating voltage: DC 3.3V-5V
Control port: Analog port
Dimension: 25mm * 28mm * 26mm
Weight: 6.7g |
**3.Components Needed**
| Control Board * 1 | USB Cable*1 | Connection Wires | LED Module*1 | Rotary Potentiometer*1 |
| -------------------------------------- | -------------------------------------- | -------------------------------------- | -------------------------------------- | -------------------------------------- |
|  |  |  |  |  |
**4.Connection Diagram**

**5.Test Program**
1.Open“KidsBlock”software to create a new project.
2.Delete the beetle role.
3.Select “Bedroom 2”:click the background library iconin the lower right corner to select Bedroom 2.

4.Select Cat, as shown below:

5.Build the program
Select “Bedroom 2”and build the program
① Background effect of the bedroom: Rotate the potentiometer, then the brightness of the background and the LED will change constantly.

②Complete program of the background room

**Program Explanation:**
The program uses the mapping function. A mapping is a functional relationship between two elements in which elements "correspond" to each other.
The brightness of the stage background from dark to normal value is in the range of 0 ~ - 200 ", and the scope of the potentiometer is "0 ~ 1023". To make the stage background brightness change constantly when rotate the potentiometer, we need to make two values correspond.
At this time, we need to read the potentiometer and divide by 5, then multiply it by negative 1 and invert it. So "0" corresponds to "0" and "1023" corresponds to "-200".
Similarly, the brightness of the LED module from dim to normal ranges from 0 to 255. Divide the potentiometer value by 5 can get the corresponding value: "0" for "0", "1023" for "255".
6.Build the program of the role
Select “Cat”and build the program
Effect of the role:When the room is dark, the cat will say "It's so dark! Turn on the light!", when the room becomes bright, the cat will say "The room lights up!", as shown below:

**6.Complete program**

**7.Test Result**
Connect the wirings and the device then select the port, then click.When we rotate the potentiometer module clockwise, the light in the bedroom goes from bright to dark, and the red LED goes from dark to bright. When we rotate clockwise to the very bottom, the cat will say: "It's so dark! Turn on the light! ".
When we rotate the module counterclockwise, the light of the bedroom slowly goes from dark to bright, and the red LED slowly goes from bright to dark. When we rotate counterclockwise to the bottom, the cat will say: "The room lights up! ", as shown in the picture below:

